package com.light.model;

import com.light.constant.Constant;
import com.light.util.SwingUtil;

import java.awt.*;

/**
 * @Author MoonlightL
 * @ClassName: Score
 * @ProjectName flappy_bird
 * @Description: 分数
 * @DateTime 2023/3/13, 0013 13:48
 */
public class Score extends GameElement {

    private int totalScore;

    private Image[] images = new Image[10];

    public Score(int x, int y, int totalScore) {
        super(x, y, SwingUtil.getImageIcon("img/font_0.png").getImage());
        this. totalScore = totalScore;

        for (int i = 0; i < 10; i++) {
            this.images[i] = SwingUtil.getImageIcon("img/font_" + i + ".png").getImage();
        }
    }

    @Override
    public void draw(Graphics2D g) {
        String str = String.valueOf(this.totalScore);
        int totalWidth = getWidth() * str.length();
        int x = (Constant.WIN_WIDTH - totalWidth) / 2;

        int index = 0;
        for (int c : str.toCharArray()) {
            Image image = this.images[c - 48];
            g.drawImage(image, x + index * getWidth() + 2, getY(), null);
            index++;
        }
    }

    @Override
    public void reset() {
        this.totalScore = 0;
    }

    public void modify(int totalScore) {
        this.totalScore = totalScore;
    }
}
